package com.example.windowdemo.window.observer;

import android.content.Context;
import android.view.View;

import com.example.windowdemo.callback.WindowObserver;
import com.example.windowdemo.window.Window;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: njb
 * @date: 2023/8/3 13:59
 * @desc:
 */
public class WindowObserverManager<T> implements WindowObserver<T> {
    private Context context;
    private List<Window<T>> windows = new ArrayList<>();

    public WindowObserverManager(Context context) {
        this.context = context;
    }

    public Window<T> createWindow(View view, T data) {
        Window<T> window = new Window<>(context);
        window.addView(view);
        window.setData(data);
        window.registerObserver(this);
        windows.add(window);
        return window;
    }

    public void removeWindow(Window<T> window) {
        window.unregisterObserver(this);
        window.hide();
        windows.remove(window);
    }

    @Override
    public void onWindowShow(Window<T> window) {

    }

    @Override
    public void onWindowHide(Window<T> window) {

    }

    @Override
    public void onWindowDataChange(Window<T> window, T data) {
        // 根据数据的变化，更新相应的视图
    }
}
